
{% from "Makefile.common.jinja" import iquote with context %}

{% set src_build_dir = e.build_dir|pjoin(src_dir|basename) %}

{#
 #   *.c *.cpp -> *.d
 #}

{% if src_dir == e.src_dir %}
	{% set cpp = (src_dir|glob('*.c', '*.cpp') + src_build_dir|glob('*.cpp'))|filemap(src_build_dir, e.names.deps) %}
{% else %}
	{% set cpp = src_dir|glob('*.c', '*.cpp')|filemap(src_build_dir, e.names.deps) %}
{% endif %}

{% for source, target in cpp.items() %}
{{ target.path }} : {{ source.path }}
	@mkdir -p {{ target.path|dirname }}
	{{v}}{{ e.cc }} {{ e.cppflags }} {{ inc_flags }} {{ iquote(source) }} -MM $^ > $@
	{# prepend build path to a target in the generated file and 
	   add .d file itself as a target so that changes in a header file would rebuild dependency files
	   See: http://make.paulandlesley.org/autodep.html #}
	@printf "{{ target.path }} {{ target.path|dirname }}{{ slash }}" | cat - $@ > $@~ && mv $@~ $@
{% endfor %}

{#
 #   *.d -> united output
 #}
{{ output_filepath }} : {{ cpp.target_paths() }}
	@echo {{ ('Scanning dependencies of ' ~ src_dir|basename)|colorize('cyan') }}
	@mkdir -p {{ output_filepath|dirname }}
	{{v}}{{ 'cat $^ >' if cpp.target_paths() else 'touch' }} $@;

all : {{ output_filepath }}
	@true

{#
vim:noexpandtab filetype=jinja
#}
